<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<%@include file="/common/head.jsp" %>

<c:set var="module" value="product"/>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
		<form action="${ctx }/admin/product/save?id=${n.id }" id="detail-form" class="form-horizontal" method="post" onsubmit="return save()">
			<div id="specification"></div>
			<c:if test="${!isFactory }">
			<div class="form-group">
                 <label class="col-sm-2 control-label">工厂</label>
                 <div class="col-sm-10">
                 	<%-- <input type="text" class="form-control" name="user.id" value="${n.user.id }"> --%>
                 	<input type="hidden" id="user-id" name="user.id" value="" class="form-control">
                 	<input type="text"  placeholder="工厂" class="form-control" id="user-name" name="user.name" value="" 
                 		onclick="bring({
						                 	url:'${ctx}/admin/user/factorySelect',
						                 	name: 'companyName',
						                 	idInput: 'user-id',
						                 	nameInput: 'user-name'
						                 	})" />
                 </div>
             </div>	
             </c:if>
			
             <div class="form-group">
             <label class="col-sm-2 control-label"></label>
             <div class="col-sm-10">
             	<input type="file" id="upload" style="display:none;" onchange="uploadFile(this)" />
                <button type="button" class="btn btn-outline btn-primary" onclick="uploadProduct();">上传产品</button>
              </div>
             </div>
		</form>
</div>

<%@include file="/common/foot.jsp" %>

<script>
function uploadProduct() {
	<c:if test="${!isFactory }">
	var factoryId = $('#user-id').val();
	if (null == factoryId || "" == factoryId) {
		error('请选择工厂');
		$('#user-id').focus();
		return;
	}
	</c:if>
	$('#upload').click(); 
}

function uploadFile(input) {
	<c:if test="${!isFactory }">
	var factoryId = $('#user-id').val();
	</c:if>
	
	var fd = new FormData();
	fd.append("file", input.files[0]);
	
	<c:if test="${!isFactory }">
	var url = "${ctx}/admin/product/uploadExcel?userId="+factoryId;
	</c:if>
	
	<c:if test="${isFactory }">
	var url = "${ctx}/admin/product/uploadExcel";
	</c:if>
	
	loading();
	$.ajax({
		url: url + '&t=' + Math.random(),
		type: "POST",
		dataType: "json",
		processData: false,
		contentType: false,
		data: fd,
		dataType: "json",
		success: function(data) {
			cancelLoading();
			if (data.success || data.statusCode == '200') {
				ok();
				reload();
			} else {
				error(data.msg);
			}
		}
	});
}
</script>
</body>
</html>
